<?php

/**
 * Simon's graphs
 *
 * Copyright (c) 2008 Jan ŠTAFA aka Simon (http://betaverze.blogspot.com)
 *
 * For more information please see http://code.google.com/
 *
 * @copyright  Copyright (c) 2008 Jan Štafa aka Simon
 * @license    GNU General Public License
 * @link       http://code.google.com/
 * @category   Graphs
 */


class Simon_Graph {
	/**
	 * Private graph data array
	 */
    private $_data = array();
      
    /**
	 * Definition type of graphs
	 */
    const LINE_CHART   = 'lc'; 
    const SPARKLINE    = 'ls';
    const BAR_CHART_H  = 'bhs';
    const BAR_CHART_V  = 'bvs';
    const BAR_CHART_HS = 'bhg';
    const BAR_CHART_VS = 'bvg';
    const PIE_CHART_2D = 'p';
    const PIE_CHART_3D = 'p3';
    const VENN_DIAGRAM = 'v';
    const SCATTER_PLOT = 's';
    const RADAR_CHART  = 'r';
    const MAP          = 't';
    const GOOGLE_METER = 'gom';
    
     
    
    /**
	 * Setter
	 * 
	 * @param mixed $name Name of variable
	 * @param mixed $value Value of variable
	 */
	public function __set($name, $value) 
    {
        $this->_data[$name] = $value;
    }

    /**
	 * Getter
	 * 
	 * @param mixed $name Key means name of variable
	 * @return mixed Value of variable
	 */
    public function __get($name) 
    {
        if (isset($this->_data[$name]))
            return $this->_data[$name];

        return null;
    }

    /**
	 * Check setting value
	 * 
	 * @param mixed $name Key means name of variable
	 * @return boolean True/false
	 */
    public function __isset($name) {
        return isset($this->_data[$name]);
    }
    
    /**
	 * Unset variable
	 * 
	 * @param mixed $type Key means name of variable to unset
	 */
    public function __unset($type) {
        unset($this->_data[$name]);
    }
    
	/**
	 * Constructor - define type of chart
	 * 
	 * @param const $type Definy type of chart
	 */
	function __construct($type=null) {

	    if($type!=null){
	        $this->__set("type",$type);
	    }
	     $this->__set("url","http://chart.apis.google.com/chart?");
		
	}
	
	
	/**
	 * Set data to display
	 * 
	 * @param $data array Set graph data to display
	 */
    function setGraphData($data){
	   
        $dataArray = ($this->__get("data")==null)? array():$this->__get("data");
        array_push($dataArray,$data);
        $this->__set("data",$dataArray);
       
	}
	
	/**
	 * Set X label
	 * 
	 * @param mixed $label1 Axis X (array or string "auto")
	 * @param mixed $label2 Axis X second (array or string "auto")
	 */
    function setXLabel($label1,$label2=null){

        $labelArray = array();
        $labelArray[1] = $label1;
        $labelArray[4] = $label2;
               
	   $this->__set("xlabel",$labelArray);
	}
	
	 
	/**
	 * Set Y label
	 * 
	 * @param mixed $label3 Axis Y left (array or string "auto")
	 * @param mixed $label4 Axis Y right (array or string "auto")
	 */
    function setYLabel($label3,$label4=null){
	   
        $labelArray = array();
        $labelArray[2] = $label3;
        $labelArray[3] = $label4;
        
	   $this->__set("ylabel",$labelArray);
	}
	

	/**
	 * Get graph fata
	 * 
	 * @return string Chd text, simple, extended encoding to define graph data
	 */
	
    private function _getData(){
    
        /*$data = $this->__get("data");
        $chd  = "&chd=t:";
        foreach ($data as $value){
            
            $chdArray[]=implode(",",$value)."";
        }
       
        $chd.=implode("|",$chdArray);
        
        return $chd;*/
        
        
        $simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        
        $data = $this->__get("data");
        $chd  = "&chd=s:";
        foreach ($data as $key=>$value){
            foreach ($value as $key) {
                
                     if($key == null){
                           $chdString.="_"; 
                     }else{
                           $chdString.=$simpleEncoding[$key];
                     }
            }
          
             $chdArray[] = $chdString; 
             $chdString  = "";
        }
       
        $chd.=implode("|",$chdArray);
        return $chd;
        
	}
	
	/**
	 * Get legend
	 * 
	 * @return string Chdl and Chco to define graph legend
	 */
    private function _getLegend(){

        $string = "";
        
        if($this->__isset("legendText")){
            $string .= "&chdl=".implode("|",$this->__get("legendText"));
        }
        
        if($this->__isset("legendColor")){
            $string .= "&chco=".implode(",",$this->__get("legendColor"));
        }
        
        return $string;
        
	}
	
	/**
	 * Get title
	 * 
	 * @return string Chtt string of graph title
	 */
    private function _getTitle(){
        
        $string = "";
        
        if($this->__isset("title")){
            $string .= "&chtt=".$this->__get("title");
        }
        
        if($this->__isset("titleColor")){
            $string .= "&chts=".$this->__get("titleColor");
        }
        
        if($this->__isset("titleColor")){
            $string .= ",".$this->__get("titleSize");
        }
        
        return $string;
        
	}
	
	/**
	 * Get size of graph
	 *
	 * @return string Chs string of graph size 
	 */
    private function _getSize(){

        $string = "&chs=";
        
        if($this->__isset("width")){
            $stringArray[] = $this->__get("width");
        }
        
        if($this->__isset("height")){
            $stringArray[] = $this->__get("height");
        }
        
        $string.=implode("x",$stringArray);
        return $string;
        
	}
	
	/**
	 * Get type of graph
	 * 
	 * @return string Cht string of graph definition
	 */
    private function _getType(){

        $string = "cht=";
        
        if($this->__isset("type")){
            $string .= $this->__get("type");
        }
        
        return $string;
        
	}
	
	/**
	 * Get label
	 * 
	 * @return string Chxt string of labels
	 */
    private function _getLabel(){
           
           $xlabel = $this->__get("xlabel");
           $ylabel = $this->__get("ylabel");
        
           $axisLabels[0] = $xlabel[1];
           $axisLabels[3] = $xlabel[4];
           $axisLabels[1] = $ylabel[2];
           $axisLabels[2] = $ylabel[3];;
           
           $chxt = "&chxt=";
               
               if(isset($axisLabels[0])){
                  $chxtArray[] = "x"; 
               }
               
               if(isset($axisLabels[1])){
                  $chxtArray[] = "y"; 
               }
               
               if(isset($axisLabels[2])){
                  $chxtArray[] = "r"; 
               }
               
               if(isset($axisLabels[3])){
                  $chxtArray[] = "x"; 
               }
           $chxt .= implode(",",$chxtArray);
          
           
           if(is_array($axisLabels[0])){
              $chxlArray[] = "0:|".implode("|",$axisLabels[0]);
           }
           
           if(is_array($axisLabels[1])){
              $chxlArray[] = "1:|".implode("|",$axisLabels[1]);
           }
           
           if(is_array($axisLabels[2])){
              $chxlArray[] = "2:|".implode("|",$axisLabels[2]);
           }
           
           if(is_array($axisLabels[3])){
              $chxlArray[] = "3:|".implode("|",$axisLabels[3]);
           }
           
           
           if(is_array($chxlArray)){
              $chxl = "&chxl=".implode("|",$chxlArray); 
           }
           
        return $chxt.$chxl;
        
	}
	
	
	/**
	 * Get url of graph
	 * 
	 * @return string Graph url to use in img tag
	 */
	public function getUrl(){
	    
	    $url  = $this->__get("url");
	    $url .= $this->_getType();
	    $url .= $this->_getSize();
	    $url .= $this->_getData();
	    $url .= $this->_getLegend();
	    $url .= $this->_getTitle();
	    $url .= $this->_getLabel();
	    
        /**
         * FIXME Pri pouziti cache je problem s mezerami, znaky $ a &amp; atd. Nutno prevest. Zachovat validitu URL!
         */
	    
	    if($this->__get("cache") === true){
	        
	        $file = $this->__get("temp")."/graph-".sha1($url).".jpg";
	      
	        if(!file_exists($file)){
	                 
	           file_put_contents($file,file_get_contents($url));   
	        
	         }
	         
	        return $file;
	         
	        
	    }else{
	        
	        return $url;
	    }
	   
	}
	
}